package com.example.wechat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.lljjcoder.citypickerview.widget.CityPicker;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener{
    private TextView cityText;
    private CityPicker citypicker;
    private EditText phone_edit,paswd_edit,surepasswd;
    private RadioGroup sex_group;
    private CheckBox read,play,music;
    private Button register;
    //定义字符串保存信息
    private String phone_str="";
    private String paswd_str="";
    private String hobby_str="1";
    private String city="";
    private String sex_str="";
    private String surepaswd_str="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }
    //组件初始化
    private void init(){
        phone_edit=findViewById(R.id.phone);
        paswd_edit=findViewById(R.id.paswd);
        sex_group=findViewById(R.id.sex);
        surepasswd=findViewById(R.id.surepaswd);
        sex_group.setOnCheckedChangeListener(this);

        read=findViewById(R.id.read_book);
        play=findViewById(R.id.play_ball);
        music=findViewById(R.id.music);
        register=findViewById(R.id.reg_register);
        register.setOnClickListener(this);



        cityText=findViewById(R.id.reg_provice);
        cityText.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.reg_provice:
                initCityPicker();

                citypicker.show();
                break;
            case R.id.reg_register:
                //获取手机号和密码
                phone_str=phone_edit.getText().toString();
                paswd_str =paswd_edit.getText().toString();
                surepaswd_str=surepasswd.getText().toString();
                hobby_str="";
                if(read.isChecked()){
                    hobby_str+=read.getText().toString();
                } if(play.isChecked()){
                hobby_str+=play.getText().toString();
            } if(music.isChecked()){
                hobby_str+=music.getText().toString();
            }
                if(paswd_str.equals(surepaswd_str)){
                    if(!phone_str.equals("")||!paswd_str.equals("")){
                        if(!city.equals("")){
                            //跳转传值
                            Intent intent=new Intent(this,SecondActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("phone",phone_str);
                            bundle.putString("paswd",paswd_str);
                            bundle.putString("sex",sex_str);
                            bundle.putString("hobby",hobby_str);
                            bundle.putString("city",city);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }else {
                            Toast.makeText
                                    (RegisterActivity.this,"请选择地址",Toast.LENGTH_LONG).show();
                        }
                    }else {
                        Toast.makeText
                                (RegisterActivity.this,"账号或密码未填写",Toast.LENGTH_LONG).show();
                    }
                }else {
                    Toast.makeText
                            (RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_LONG).show();
                }


                break;


        }

    }
    public void initCityPicker(){

        citypicker=new CityPicker.Builder(RegisterActivity.this)
                .textSize(16)
                .province("xx省")
                .city("xx市")
                .district("xx区")
                .build();
        citypicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... strings) {
                String p=strings[0];
                String c=strings[1];
                String d=strings[2];
                cityText.setText(String.format("%s %s %s",p,c,d));
                city=p+c+d;

            }

            @Override
            public void onCancel() {

            }
        });




    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        sex_str= i == R.id.nan? "男性":"女性";

    }
}
